/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.regions;

import ca.trader.utils.TraderTreeSet;
import java.io.Serializable;
import java.util.Iterator;
import javax.persistence.Embeddable;
import javax.persistence.Transient;

/**
 * Class to represent a region.  Contains the name of the region and the cities in that region.  The cities in that region
 * are only available if set during constructing the region object.
 * You can iterate over an instance of this class, which will return all cities in this region.
 * @author Ahsan
 */
@Embeddable
public class Region implements Iterable<String>, Comparable<Region>, Serializable
{
	 /* Name of the region */
	 private String regionName;
	 
	 /* Cities in the region */
	 @Transient
	 private TraderTreeSet<String> cities = new TraderTreeSet<>();
	 
	 /**
	  * Initiate a region with a null name and no cities
	  */
	 public Region()
	 {
	 }
	 
	 /**
	  * initiate a region with a name and no cities
	  * @param name the name of the region
	  */
	 public Region(String name)
	 {
		  this.regionName = name;
	 }

	 /**
	  * Add a city to the region
	  * @param city the city to add
	  */
	 public void addCity(String city)
	 {
		  cities.add(city);
	 }

	 /**
	  * Get the name of the region
	  * @return the name of the region. Returns null if no region name has been set.
	  */
	 public String getRegionName()
	 {
		  return regionName;
	 }

	 /**
	  * Adds a list of the cities to the region
	  * @param cities the list of cities
	  */
	 public void addCities(String[] cities)
	 {
		  for (String city : cities)
		  {
			   addCity(city);
		  }
	 }

	 /*
	  * Returns all the cities in this region.
	  * @return all the cities in the region.
	  */
	 public String[] getCityList()
	 {
		  return cities.toArray(new String[0]);
	 }

	 /**
	  * returns true if the region contains the given city
	  * @param city the city to search
	  * @return true if the city exists.  False if not.
	  */
	 public boolean containsCity(String city)
	 {
		  return cities.contains(city);
	 }
	 
	 
	 /**
	  * Compares this Region object to another object.
	  * @param o the other object
	  * @return true if the other object is an instance of Region, and has the same name as this object.  False otherwise.
	  */
	 @Override
	 public boolean equals(Object o)
	 {
		  if (!(o instanceof Region))
		  {
			   return false;
		  }

		  return this.regionName.equals(((Region) o).getRegionName());
	 }

	 /**
	  * Returns an iterator over all the cities in the region
	  * @return an iterator over all the cities in the region
	  */
	 @Override
	 public Iterator<String> iterator()
	 {
		  return cities.iterator();
	 }
	 
	 /**
	  * Compares the name of this region to the name of another region
	  * @param o The other region
	  * @return string comparison of the name of the region.
	  */
	 @Override
	 public int compareTo(Region o)
	 {
		  return this.getRegionName().compareTo(o.getRegionName());
	 }
	 
	 /**
	  * 
	  * @return the name of the region
	  */
	 @Override
	 public String toString()
	 {
		  return this.getRegionName();
	 }

}
